<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
%>
<html>
<head>
	<base href="<%=basePath%>">
<meta charset="UTF-8">

<link href="jquery/bootstrap_3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="jquery/bootstrap-datetimepicker-master/css/bootstrap-datetimepicker.min.css" type="text/css" rel="stylesheet" />
	<link href="jquery/bs_pagination-master/css/jquery.bs_pagination.min.css" type="text/css" rel="stylesheet" />

<script type="text/javascript" src="jquery/jquery-1.11.1-min.js"></script>
<script type="text/javascript" src="jquery/bootstrap_3.3.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="jquery/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.min.js"></script>
<script type="text/javascript" src="jquery/bootstrap-datetimepicker-master/locale/bootstrap-datetimepicker.zh-CN.js"></script>
<%--	分页插件--%>

<script type="text/javascript" src="jquery/bs_pagination-master/js/jquery.bs_pagination.min.js"></script>
<script type="text/javascript" src="jquery/bs_pagination-master/localization/en.js"></script>

<script type="text/javascript">

	$(function(){

		//定制字段
		$("#definedColumns > li").click(function(e) {
			//防止下拉菜单消失
	        e.stopPropagation();
	    });

		//分页查询
		function queryCustomerForPage(pageNo, pageSize) {  //分页查询  ,pageNo:第几页   pageSize:每页条数
			var name = $("#query-name").val();
			var owner = $("#query-owner").val();
			var phone = $("#query-phone").val();
			var  website= $("#query-website").val();
			// var pageNo=1;
			// var pageSize=10;
			$.ajax({
				url: 'workbench/customer/queryCustomerForPageByCondition.do',
				data: {
					name: name,
					owner: owner,
					phone:phone,
					website:website,
					pageNo: pageNo,
					pageSize: pageSize
				},
				type: "post",
				dataType: 'json',
				success: function (data) {
					//显示总条数
					$("#totalRowsB").text(data.retData.totalRows);

					//显示客户列表：
					var htmlStr = "";
					$.each(data.retData.customerList, function (index, obj) {
						htmlStr+='<tr>';
						htmlStr+='	<td><input type="checkbox" value="'+obj.id+'"/></td>';
						htmlStr+='	<td><a style="text-decoration: none; cursor: pointer;" onclick="window.location.href=\'workbench/customer/queryCustomerForDetail.do?customerId='+obj.id+'\';">'+obj.name+'</a></td>';
						htmlStr+='	<td>'+obj.owner+'</td>';
						htmlStr+='	<td>'+obj.phone+'</td>';
						htmlStr+='	<td>'+obj.website+'</td>';
						htmlStr+='</tr>';
					});
					$("#tBody").html(htmlStr);
					$("#checkAll").prop("checked", false);
					var totalPages = 1;
					if (data.retData.totalRows % pageSize == 0) { //可以整除
						totalPages = data.retData.totalRows / pageSize;
					} else { //不可以整除 ，取整加一
						totalPages = parseInt(data.retData.totalRows / pageSize) + 1;
					}
					//显示翻页信息：
					$("#demo_page1").bs_pagination({
						currentPage: pageNo,      //第几页
						rowsPerPage: pageSize,   //每页条数
						totalRows: data.retData.totalRows,  //总条数
						totalPages: totalPages,   //总页数（必填）
						visiblePageLinks: 5,    //显示的按钮卡片数
						showGoToPage: true,
						showRowsPerPage: true,
						showRowsInfo: true,
						//每次触发切换页面 自动触动函数
						onChangePage: function (event, pageObj) {
							queryCustomerForPage(pageObj.currentPage, pageObj.rowsPerPage);
						}
					});
				}
			});
		}

		//加载页面，默认查询第一页数据，及总条数 ，默认每页10条
		queryCustomerForPage(1, 5);
		//点击查询按钮事件：
		$("#queryBtn").click(function (){
			var pageSize = $("#demo_page1").bs_pagination("getOption", "rowsPerPage");  //分页的每页大小获取参数
			queryCustomerForPage(1,pageSize);
		})

		//全选按钮点击事件
		$("#checkAll").click(function () {
			$("#tBody input[type='checkbox']").prop("checked", this.checked);  // tBody下的属性为checkbox的input标签
		});
		//有一个checkbox没选中时，将 全选的按钮框取消
		$("#tBody").on("click", "input[type='checkbox']", function () {
			if ($("#tBody input[type='checkbox']:checked").size() == $("#tBody input[type='checkbox']").size()) {
				$("#checkAll").prop("checked", true);
			} else {
				$("#checkAll").prop("checked", false);
			}
		});


		//创建模态窗口的保存按钮点击事件
		$("#saveCreateCustomer").click(function (){
			//获取参数
			var owner=$("#create-owner").val();
			var name=$("#create-name").val();
			var website=$("#create-website").val();
			var phone=$("#create-phone").val();
			var description=$("#create-description").val();
			var contactSummary=$("#create-contactSummary").val();
			var nextContactTime=$("#create-nextContactTime").val();
			var address=$("#create-address").val();
			//验证：
			if (name==""){
				alert("名称不能为空");
				return;
			}if (nextContactTime!=""){
				nextContactTime.replaceAll("/","-");
			}
			$.ajax({
				url:'workbench/customer/saveCreateCustomer.do',
				type:'post',
				data:{
					owner:owner,
					name:name,
					website:website,
					phone:phone,
					description:description,
					contactSummary:contactSummary,
					nextContactTime:nextContactTime,
					address:address,
				},
				dataType:'json',
				success:function(data){
					if (data.code=='1'){
						$("#createCustomerModal").modal("hide");
						var pageSize = $("#demo_page1").bs_pagination("getOption", "rowsPerPage");  //分页的每页大小获取参数
						var currentPage=$("#demo_page1").bs_pagination("getOption", "currentPage"); //当前分页的第几页
						queryCustomerForPage(currentPage,pageSize);
					}else{
						$("#createCustomerModal").modal("show");
						alert(data.message);
					}
				}

			})
		})

		//修改按钮 的点击事件
		$("#editCustomerBtn").click(function (){
			var checkbox=$("#tBody input[type='checkbox']:checked");
			if (checkbox.size()>1){
				alert("只能选择一个进行修改");
				return;
			}else if(checkbox.size()==0){
				alert("请选择要修改的客户");
				return;
			}
			var customerId=checkbox.val();
			$("#hiddenEditCustomerId").val(customerId);
			$.ajax({
				url:'workbench/customer/queryCustomerByCustomerId.do',
				type:'post',
				dataType:'json',
				data:{
					customerId:customerId,
				},
				success:function (customer){
					$("#edit-owner").val(customer.owner);
					$("#edit-name").val(customer.name);
					$("#edit-website").val(customer.website);
					$("#edit-description").val(customer.description);
					$("#edit-phone").val(customer.phone);
					$("#edit-contactSummary").val(customer.contactSummary);
					$("#edit-nextContactTime").val(customer.nextContactTime);
					$("#edit-address").val(customer.address);
					$("#editCustomerModal").modal("show");
				}
			})

		});
		//修改的模态窗口  保存按钮事件
		$("#saveEditCustomerBtn").click(function (){
			//获取参数
			var id=$("#hiddenEditCustomerId").val();
			var owner=$("#edit-owner").val();
			var name=$("#edit-name").val();
			var website=$("#edit-website").val();
			var description=$("#edit-description").val();
			var phone=$("#edit-phone").val();
			var contactSummary=$("#edit-contactSummary").val();
			var nextContactTime=$("#edit-nextContactTime").val();
			var addres=$("#edit-address").val();
			if (name==""){
				alert("名称不能为空");
				return;
			}if (nextContactTime!=""){
				nextContactTime.replaceAll("/","-");
			}
			$.ajax({
				url:'worckbench/customer/saveEditCustomer.do',
				type:'post',
				data:{
					id:id,
					owner:owner,
					name:name,
					website:website,
					description:description,
					phone:phone,
					contactSummary:contactSummary,
					nextContactTime:nextContactTime,
					addres:addres,
				},
				dataType:'json',
				success:function (data){
					if (data=="1"){
						$("#editCustomerModal").modal("hide");
						var pageSize = $("#demo_page1").bs_pagination("getOption", "rowsPerPage");  //分页的每页大小获取参数
						var currentPage=$("#demo_page1").bs_pagination("getOption", "currentPage"); //当前分页的第几页
						queryCustomerForPage(currentPage,pageSize);
					}else{
						alert("修改失败，请联系管理员");
						$("#editCustomerModal").modal("show");
					}
				}
			})
		});

		//删除按钮事件：
		$("#deleteCustomerBtn").click(function (){
			var checkbox=$("#tBody input[type='checkbox']:checked");
			if(checkbox.size()==0){
				alert("请选择要删除的客户");
				return;
			}
			if (confirm("确定要删除吗")) {
				//获取id值
				var idStr="";
				$.each(checkbox,function (){
					idStr+="id="+this.value+"&";
				})
				var id=idStr.substr(0,idStr.length-1);
				$.ajax({
					url:'workbench/customer/deleteCustomerByIds.do',
					type:'post',
					data:id,
					dataType:'json',
					success:function (data){
						if (data=="0"){
							alert("删除失败");
						}else{
							var pageSize = $("#demo_page1").bs_pagination("getOption", "rowsPerPage");  //分页的每页大小获取参数
							var currentPage=$("#demo_page1").bs_pagination("getOption", "currentPage"); //当前分页的第几页
							queryCustomerForPage(currentPage,pageSize);
						}
					}
				})
			}
		});

	});

</script>
</head>
<body>

	<!-- 创建客户的模态窗口 -->
	<div class="modal fade" id="createCustomerModal" role="dialog">
		<div class="modal-dialog" role="document" style="width: 85%;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="myModalLabel1">创建客户</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form">

						<div class="form-group">
							<label for="create-owner" class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
							<div class="col-sm-10" style="width: 300px;">
								<select class="form-control" id="create-owner">
									<c:forEach items="${userList}" var="user">
										<option value="${user.id}">${user.name}</option>
									</c:forEach>
<%--								  <option>zhangsan</option>--%>
<%--								  <option>lisi</option>--%>
<%--								  <option>wangwu</option>--%>
								</select>
							</div>
							<label for="create-name" class="col-sm-2 control-label">名称<span style="font-size: 15px; color: red;">*</span></label>
							<div class="col-sm-10" style="width: 300px;">
								<input type="text" class="form-control" id="create-name">
							</div>
						</div>

						<div class="form-group">
                            <label for="create-website" class="col-sm-2 control-label">公司网站</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="create-website">
                            </div>
							<label for="create-phone" class="col-sm-2 control-label">公司座机</label>
							<div class="col-sm-10" style="width: 300px;">
								<input type="text" class="form-control" id="create-phone">
							</div>
						</div>
						<div class="form-group">
							<label for="create-description" class="col-sm-2 control-label">描述</label>
							<div class="col-sm-10" style="width: 81%;">
								<textarea class="form-control" rows="3" id="create-description"></textarea>
							</div>
						</div>
						<div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative;"></div>

                        <div style="position: relative;top: 15px;">
                            <div class="form-group">
                                <label for="create-contactSummary" class="col-sm-2 control-label">联系纪要</label>
                                <div class="col-sm-10" style="width: 81%;">
                                    <textarea class="form-control" rows="3" id="create-contactSummary"></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="create-nextContactTime" class="col-sm-2 control-label">下次联系时间</label>
                                <div class="col-sm-10" style="width: 300px;">
                                    <input type="date" class="form-control" id="create-nextContactTime">
                                </div>
                            </div>
                        </div>

                        <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative; top : 10px;"></div>

                        <div style="position: relative;top: 20px;">
                            <div class="form-group">
                                <label for="create-address" class="col-sm-2 control-label">详细地址</label>
                                <div class="col-sm-10" style="width: 81%;">
                                    <textarea class="form-control" rows="1" id="create-address"></textarea>
                                </div>
                            </div>
                        </div>
					</form>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="saveCreateCustomer" type="button" class="btn btn-primary" data-dismiss="modal">保存</button>
				</div>
			</div>
		</div>
	</div>

	<!-- 修改客户的模态窗口 -->
	<div class="modal fade" id="editCustomerModal" role="dialog">
		<div class="modal-dialog" role="document" style="width: 85%;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title" id="myModalLabel">修改客户</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" role="form">
						<input type="hidden" value="" id="hiddenEditCustomerId">
						<div class="form-group">
							<label for="edit-owner" class="col-sm-2 control-label">所有者<span style="font-size: 15px; color: red;">*</span></label>
							<div class="col-sm-10" style="width: 300px;">
								<select class="form-control" id="edit-owner">
									<c:forEach items="${userList}" var="user">
										<option value="${user.id}">${user.name}</option>
									</c:forEach>
<%--									<option>zhangsan</option>--%>
<%--									<option>lisi</option>--%>
<%--									<option>wangwu</option>--%>
								</select>
							</div>
							<label for="edit-name" class="col-sm-2 control-label">名称<span style="font-size: 15px; color: red;">*</span></label>
							<div class="col-sm-10" style="width: 300px;">
								<input type="text" class="form-control" id="edit-name" value="动力节点">
							</div>
						</div>

						<div class="form-group">
                            <label for="edit-website" class="col-sm-2 control-label">公司网站</label>
                            <div class="col-sm-10" style="width: 300px;">
                                <input type="text" class="form-control" id="edit-website" value="http://www.bjpowernode.com">
                            </div>
							<label for="edit-phone" class="col-sm-2 control-label">公司座机</label>
							<div class="col-sm-10" style="width: 300px;">
								<input type="text" class="form-control" id="edit-phone" value="010-84846003">
							</div>
						</div>

						<div class="form-group">
							<label for="edit-description" class="col-sm-2 control-label">描述</label>
							<div class="col-sm-10" style="width: 81%;">
								<textarea class="form-control" rows="3" id="edit-description"></textarea>
							</div>
						</div>

						<div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative;"></div>

                        <div style="position: relative;top: 15px;">
                            <div class="form-group">
                                <label for="edit-contactSummary" class="col-sm-2 control-label">联系纪要</label>
                                <div class="col-sm-10" style="width: 81%;">
                                    <textarea class="form-control" rows="3" id="edit-contactSummary"></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="edit-nextContactTime" class="col-sm-2 control-label">下次联系时间</label>
                                <div class="col-sm-10" style="width: 300px;">
                                    <input type="date" class="form-control" id="edit-nextContactTime">
                                </div>
                            </div>
                        </div>

                        <div style="height: 1px; width: 103%; background-color: #D5D5D5; left: -13px; position: relative; top : 10px;"></div>

                        <div style="position: relative;top: 20px;">
                            <div class="form-group">
                                <label for="edit-address" class="col-sm-2 control-label">详细地址</label>
                                <div class="col-sm-10" style="width: 81%;">
                                    <textarea class="form-control" rows="1" id="edit-address">北京大兴大族企业湾</textarea>
                                </div>
                            </div>
                        </div>
					</form>

				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					<button id="saveEditCustomerBtn" type="button" class="btn btn-primary" data-dismiss="modal">更新</button>
				</div>
			</div>
		</div>
	</div>




	<div>
		<div style="position: relative; left: 10px; top: -10px;">
			<div class="page-header">
				<h3>客户列表</h3>
			</div>
		</div>
	</div>

	<div style="position: relative; top: -20px; left: 0px; width: 100%; height: 100%;">

		<div style="width: 100%; position: absolute;top: 5px; left: 10px;">

			<div class="btn-toolbar" role="toolbar" style="height: 80px;">
				<form class="form-inline" role="form" style="position: relative;top: 8%; left: 5px;">

				  <div class="form-group">
				    <div class="input-group">
				      <div class="input-group-addon">名称</div>
				      <input class="form-control" type="text" id="query-name">
				    </div>
				  </div>

				  <div class="form-group">
				    <div class="input-group">
				      <div class="input-group-addon">所有者</div>
				      <input class="form-control" type="text" id="query-owner">
				    </div>
				  </div>

				  <div class="form-group">
				    <div class="input-group">
				      <div class="input-group-addon">公司座机</div>
				      <input class="form-control" type="text" id="query-phone">
				    </div>
				  </div>

				  <div class="form-group">
				    <div class="input-group">
				      <div class="input-group-addon">公司网站</div>
				      <input class="form-control" type="text" id="query-website">
				    </div>
				  </div>

				  <button type="button" class="btn btn-default" id="queryBtn">查询</button>

				</form>
			</div>
			<div class="btn-toolbar" role="toolbar" style="background-color: #F7F7F7; height: 50px; position: relative;top: 5px;">
				<div class="btn-group" style="position: relative; top: 18%;">
				  <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#createCustomerModal"><span class="glyphicon glyphicon-plus"></span> 创建</button>
				  <button type="button" class="btn btn-default" id="editCustomerBtn"><span class="glyphicon glyphicon-pencil"></span> 修改</button>
				  <button type="button" class="btn btn-danger" id="deleteCustomerBtn"><span class="glyphicon glyphicon-minus"></span> 删除</button>
				</div>

			</div>
			<div style="position: relative;top: 10px;">
				<table class="table table-hover">
					<thead>
						<tr style="color: #B3B3B3;">
							<td><input type="checkbox" id="checkAll"/></td>
							<td>名称</td>
							<td>所有者</td>
							<td>公司座机</td>
							<td>公司网站</td>
						</tr>
					</thead>
					<tbody id="tBody">
<%--						<tr>--%>
<%--							<td><input type="checkbox" /></td>--%>
<%--							<td><a style="text-decoration: none; cursor: pointer;" onclick="window.location.href='detail.jsp';">动力节点</a></td>--%>
<%--							<td>zhangsan</td>--%>
<%--							<td>010-84846003</td>--%>
<%--							<td>http://www.bjpowernode.com</td>--%>
<%--						</tr>--%>
<%--                        <tr class="active">--%>
<%--                            <td><input type="checkbox" /></td>--%>
<%--                            <td><a style="text-decoration: none; cursor: pointer;" onclick="window.location.href='detail.jsp';">动力节点</a></td>--%>
<%--                            <td>zhangsan</td>--%>
<%--                            <td>010-84846003</td>--%>
<%--                            <td>http://www.bjpowernode.com</td>--%>
<%--                        </tr>--%>
					</tbody>
				</table>
				<div id="demo_page1"></div>
			</div>

<%--			<div style="height: 50px; position: relative;top: 30px;" >--%>
<%--				<div>--%>
<%--					<button type="button" class="btn btn-default" style="cursor: default;">共<b id="totalRowsB"></b>条记录</button>--%>
<%--				</div>--%>
<%--				<div class="btn-group" style="position: relative;top: -34px; left: 110px;">--%>
<%--					<button type="button" class="btn btn-default" style="cursor: default;">显示</button>--%>
<%--					<div class="btn-group">--%>
<%--						<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">--%>
<%--							10--%>
<%--							<span class="caret"></span>--%>
<%--						</button>--%>
<%--						<ul class="dropdown-menu" role="menu">--%>
<%--							<li><a href="#">20</a></li>--%>
<%--							<li><a href="#">30</a></li>--%>
<%--						</ul>--%>
<%--					</div>--%>
<%--					<button type="button" class="btn btn-default" style="cursor: default;">条/页</button>--%>
<%--				</div>--%>
<%--				<div style="position: relative;top: -88px; left: 285px;">--%>
<%--					<nav>--%>
<%--						<ul class="pagination">--%>
<%--							<li class="disabled"><a href="#">首页</a></li>--%>
<%--							<li class="disabled"><a href="#">上一页</a></li>--%>
<%--							<li class="active"><a href="#">1</a></li>--%>
<%--							<li><a href="#">2</a></li>--%>
<%--							<li><a href="#">3</a></li>--%>
<%--							<li><a href="#">4</a></li>--%>
<%--							<li><a href="#">5</a></li>--%>
<%--							<li><a href="#">下一页</a></li>--%>
<%--							<li class="disabled"><a href="#">末页</a></li>--%>
<%--						</ul>--%>
<%--					</nav>--%>
<%--				</div>--%>
<%--			</div>--%>

		</div>

	</div>
</body>
</html>
